package ru.bragnikita.utils.logexplorer.web;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Nikita.Brazhnikov
 */
@FacesConverter("LongTimeConverter")
public class LongTimeConverter implements Converter {

	private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd/MM");

	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
		return null;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
		if (o == null) {
			return "";
		}
		if (o instanceof Long) {
			Long time = (Long) o;
			Date date = new Date(time);
			return format.format(date);
		}
		if (o instanceof Date) {
			return format.format(o);
		}
		return "";
	}
}
